package org.algorithm.落雪.链表;

import org.algorithm.biubiu.wheel_1.common.ListNode;

/**
 * @Date 2022/2/27
 * @Link {https://leetcode-cn.com/problems/merge-k-sorted-lists/}
 */
public class 合并K个升序链表 {


    
    public ListNode mergeKLists(ListNode[] lists) {

        ListNode list = new ListNode(-1);
        ListNode res = list;
        int k = lists.length;
        while(true){
            int f=0;
            int min = Integer.MAX_VALUE;
            for(int i = 0;i<k;++i){
                if(lists[i]!=null&&lists[i].val<min){
                    min = lists[i].val;
                    f=1;
                }
            }
            for(int i = 0;i<k;++i){
                if(lists[i]!=null&&lists[i].val==min){
                    lists[i] = lists[i].next;
                    break;
                }
            }
            if(f==0){
                break;
            }
            ListNode tmp = new ListNode(min);
            list.next = tmp;
            list = list.next;
        }
        return res.next;

    }

}
